/*
 * @Copyright: (C) 2019 Zhang Rida. All rights reserved.
 *
 * @Author: Zhang Rida
 * @Email: 215154317@qq.com
 * @FilePath: /WEILA_SDK_MODULE/include/wl_malloc.h
 * @Date: 2021-02-18 15:07:07
 * @Description: malloc for weila osal
 *
 * @LastEditors: Zhang Rida
 * @LastEditTime: 2021-09-17 10:49:04
 */

#ifndef _WL_MALLOC_H
#define _WL_MALLOC_H

#include "wl_types.h"

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

#define WL_MEMORY_POOL

#if defined(WL_MEMORY_POOL)
#    if defined(WL_OS_MG)
#        define WL_MEMORY_POOL_SIZE (600 * 1024)  //内存池大小
#    elif defined(WL_OS_U8910)
#        define WL_MEMORY_POOL_SIZE (1024 * 1024)  //内存池大小
#    elif defined(WL_OS_QUECTEL)
#        define WL_MEMORY_POOL_SIZE (1024 * 1024)  //内存池大小
#    else                                          // default linux
#        define WL_MEMORY_POOL_SIZE (1024 * 1024)  //内存池大小
#    endif
#    define WL_MEMORY_STREAM_CACHE_MAX (WL_MEMORY_POOL_SIZE / 16)  // protocol协议流缓存区大小
#    define WL_MEMORY_CORE_THRESHOLD (256 * 1024)  //未分配内存达到系统临界(不小于64k内存,以保证系统能正常工作)
//未分配内存达到PTT临界,停止录音、消息接收(不小于64k内存,以保证播放器能正常工作)
#    define WL_MEMORY_PTT_THRESHOLD (WL_MEMORY_CORE_THRESHOLD + 128 * 1024)
#    define WL_MEMORY_PTT_CACHE_MAX (WL_MEMORY_POOL_SIZE - WL_MEMORY_PTT_THRESHOLD)  // PTT语音缓存区大小
#else
#    define WL_MEMORY_POOL_SIZE 0
#    define WL_MEMORY_STREAM_CACHE_MAX (64 * 1024)  // protocol协议流缓存区大小
#    define WL_MEMORY_CORE_THRESHOLD (256 * 1024)  //未分配内存达到系统临界(不小于该内存,以保证系统能正常工作)
//未分配内存达到PTT临界,停止录音、消息接收(不小于64k内存,以保证播放器能正常工作)
#    define WL_MEMORY_PTT_THRESHOLD (WL_MEMORY_CORE_THRESHOLD + 256 * 1024)
#    define WL_MEMORY_PTT_CACHE_MAX (1024 * 1024 - WL_MEMORY_PTT_THRESHOLD)  // PTT语音缓存区大小
#endif
#define WL_MEMORY_SYSTEM_THRESHOLD 300 * 1024  //系统内存临界值,低于该值半夜重启
#define WL_MEMORY_SYSTEM_ALARM_SIZE 50 * 1024  //系统内存告警值,低于该值立即重启

#ifdef WL_MALLOC_CHECK  //内存泄露检查时使用
#    define wl_malloc(size) wl_mem_malloc_debug((size), __FUNCTION__, __LINE__)
#    define wl_free(ptr) wl_mem_free_debug((ptr), __FUNCTION__, __LINE__)
#else
#    define wl_malloc(size) wl_mem_malloc(size)
#    define wl_free(ptr) wl_mem_free(ptr)
#endif
#define wl_realloc(ptr, size) wl_mem_realloc(ptr, size)

wl_bool wl_mem_init(void);
void    wl_mem_end(void);
#ifdef WL_MALLOC_CHECK
void* wl_mem_malloc_debug(wl_size size, const wl_char* func, wl_uint32 line);
void  wl_mem_free_debug(void* ptr, const wl_char* func, wl_uint32 line);
#endif
void*   wl_mem_malloc(wl_size size);
void    wl_mem_free(void* ptr);
void*   wl_mem_realloc(void* ptr, wl_size size);
wl_size wl_mem_size(void* ptr);
wl_size wl_mem_num_free(void);
wl_size wl_mem_num_used(void);
wl_size wl_mem_num_fresh(void);
wl_size wl_mem_free_size(void);
wl_size wl_mem_used_size(void);
wl_size wl_mem_fresh_size(void);
wl_size wl_mem_sys_size(void);
wl_bool wl_mem_check(void);

#define WL_MEMORY_SIZE_LIMIT (128 * 1024)  // 内存块最大Size
#define WL_MEMORY_MEDIA_IS_THRESHOLD ((wl_mem_fresh_size() + wl_mem_free_size()) < WL_MEMORY_CORE_THRESHOLD)
#define WL_MEMORY_PTT_IS_THRESHOLD ((wl_mem_fresh_size() + wl_mem_free_size()) < WL_MEMORY_PTT_THRESHOLD)
#define WL_MEMORY_PTT_SINGLE_IS_THRESHOLD                                                                              \
    ((wl_mem_fresh_size() + wl_mem_free_size()) < (WL_MEMORY_PTT_THRESHOLD + 64 * 1024))  //内存到达单呼临界

#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* _WL_MALLOC_H */
